package com.example.howard.xiao7music.ui.fragment.LocalFrament;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.presenter.LocalSongPresenter;
import com.example.howard.xiao7music.ui.Activity.LocalActivity.FolderSongActivity;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.LocalAdapter.LocalFolderAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/29.
 */
public class LocalFolder extends Fragment{
    private LocalSongPresenter localSongPresenter=new LocalSongPresenter();
    private List<Map<String, Object>> folderlist=new ArrayList<>();
    private LocalFolderAdapter myAdapter;
    private ListView listView;
    public AdapterView.OnItemClickListener listener;
    private MyApplication myApplication;
    private BottomPlayer bottomPlayer;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.local_folder, null);
        listView= (ListView) view.findViewById(R.id.folder_info);
        myApplication= (MyApplication) getActivity().getApplication();
        folderlist=localSongPresenter.getFolderlist(getContext());
        refreshListView();
        return view;
    }

    private void refreshListView(){
        myAdapter = new LocalFolderAdapter(getContext(), folderlist);
        listView.setAdapter(myAdapter);
        listener= resetLickListener();
        listView.setOnItemClickListener(listener);
    }

    private AdapterView.OnItemClickListener resetLickListener(){
        AdapterView.OnItemClickListener folderListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                String path= folderlist.get(arg2).get("PATH").toString();
                String folder=folderlist.get(arg2).get("FOLDERNAME").toString();
                Intent intent = new Intent(getActivity(), FolderSongActivity.class);
                intent.putExtra("PATH", path);
                intent.putExtra("FOLDERNAME",folder);
                startActivity(intent);

            }
        };
        return folderListclickListener;
    }

}
